/**
 * @author raymondlo
 */
exports.AAActionManager = Core.Class.singleton({
/** @lends Animation.AAActionManager.prototype */
	classname: 'AAActionManager',
	
	/**
	 * @class A singleton that manages all actions. 
	 * @constructs The default constructor.
	 * @augments Core.Class
	 */
    initialize: function(){
    	this.actions = new Array();
		this.updateListener = new this.AAUpdateListener( this );
		Core.UpdateEmitter.addListener(this.updateListener, this.updateListener.onUpdate);
    },
	
	/**
	 * Listener which handles all active actions.
	 */
	AAUpdateListener: Core.MessageListener.subclass({
    
        classname: 'AAUpdateListener',
        
        initialize: function(manager){
            this.manager = manager;
        },
           
        onUpdate: function(t){
			for (var i=0; i < this.manager.actions.length; i++) {
				//runs the action
				var act = this.manager.actions[i];
				act.action.update(t, act.node);
				if (act.action.isDone()) {
					//remove action from array
					this.manager.actions.splice(i,1);
				}
			}
        },
    }),
	
	/**
	 * Adds an action with a target. This function normally would only be called from AANode.runAction().
	 * Pushes an action into an array and starts the action.
	 *
	 * @param {AANode.Class} node The node that's performing the action
	 * @param {AAIntervalAction.Class} action The action to be performed
	 * @see AANode#runAction
	 */
	addAction: function(node,action) {
		action.startAction(node);
		this.actions.push(new AAElement(node,action));
	},
	
	/**
	 * Removes all actions in a target.
	 *
	 * @param {AANode.Class} node The node to remove all actions from
	 * @see AANode#stopAllActions
	 */
	removeAllActionsFromTarget: function(node) {
		for (var i=0; i < this.actions.length; i++) {
			var act = this.actions[i];
			if (node == act.node) {
				//console.log("zzzz::: removeAllActionsFromTarget"+i);
				this.actions.splice(i,1);
			}
		}
	},
	
	/**
	 * The default destructor.
	 */
	destroy: function(){
		Core.UpdateEmitter.removeListener(this.updateListener);
		this.updateListener.destroy();
    },
});


var AAElement = exports.AAElement = Core.Class.subclass({
	initialize: function(node,action){
		this.node = node;
		this.action = action;
    },
});
